using SimpleJSON;
using UnityEngine;

namespace Gj
{
    public class DeepKeyHelper : NodeHelper
    {
        public BindDataType dataType;
        public NodeHelper helper;
        public NodeHelper[] helpers;

        protected override BindDataType bindDataType => BindDataType.Object;

        protected override void BindData(JSONObject obj)
        {
            if (!StringTools.IsEmpty(key))
            {
                var _d = obj[key];
                switch (dataType)
                {
                    case BindDataType.String:
                        if (helper != null) helper.SetData(_d.Value);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(_d.Value);
                        }
                        break;
                    case BindDataType.Int:
                        if (helper != null) helper.SetData(_d.AsInt);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(_d.AsInt);
                        }
                        break;
                    case BindDataType.Float:
                        if (helper != null) helper.SetData(_d.AsFloat);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(_d.AsFloat);
                        }
                        break;
                    case BindDataType.Bool:
                        if (helper != null) helper.SetData(_d.AsBool);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(_d.AsBool);
                        }
                        break;
                    case BindDataType.Object:
                        if (helper != null) helper.SetData(_d.AsObject);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(_d.AsObject);
                        }
                        break;
                    case BindDataType.List:
                        if (helper != null) helper.SetData(_d.AsArray);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(_d.AsArray);
                        }
                        break;
                    case BindDataType.None:
                        SetChildData(_d.AsObject);
                        break;
                }
            }
        }

        protected override void BindData(string s)
        {
            SetKey(s);
        }
    }
}